package com.wjj.app.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.infrastructure.utils.LogUtils;
import com.wjj.app.R;
import com.wjj.app.fragment.viewpage1.Fragment1;
import com.wjj.app.fragment.viewpage1.Fragment2;
import com.wjj.app.fragment.viewpage1.FragmentAdd1;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wjj on 16/12/8.
 */
public class MainFragment1 extends Fragment {
    private ViewPager viewPager;

    private Fragment1 fragment1;
    private Fragment2 fragment2;

    private TextView tv1;
    private TextView tv2;

    private List<Fragment> list = new ArrayList<>();

    private View view;

    private FragmentStatePagerAdapter fragmentStatePagerAdapter;

    private LinearLayout titleLl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        } else {
            view = inflater.inflate(R.layout.fragment1, container, false);
            viewPager = (ViewPager) view.findViewById(R.id.fviewpage1);
            titleLl = (LinearLayout) view.findViewById(R.id.title_ll);

            tv1 = (TextView) view.findViewById(R.id.fmenu_1);
            tv2 = (TextView) view.findViewById(R.id.fmenu_2);

            tv1.setOnClickListener(new rlListener(0));
            tv2.setOnClickListener(new rlListener(1));
            initFragment();

        }
        tabMenuClicked(0);
        return view;
    }

    public void add2() {
        FragmentAdd1 fragmentAdd1 = new FragmentAdd1();
        list.add(fragmentAdd1);
        fragmentStatePagerAdapter.notifyDataSetChanged();
        viewPager.setCurrentItem(2);

        fragmentAdd1.setListenerA(new FragmentAdd1.MyInterfaceA() {
            @Override
            public void remove() {
                remove2();
            }
        });
    }

    public void remove2() {
        if (list.size() > 2) {
            list.remove(2);
            fragmentStatePagerAdapter.notifyDataSetChanged();
            viewPager.setCurrentItem(1);
        }


    }


    public class rlListener implements View.OnClickListener {
        private int index = 0;

        public rlListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
    }

    private void initFragment() {
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        list.add(fragment1);
        list.add(fragment2);

        fragment2.setListener(new Fragment2.MyInterface() {
            @Override
            public void add() {
                add2();
            }
        });


        fragmentStatePagerAdapter = new FragmentStatePagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public int getItemPosition(Object object) {
//                return super.getItemPosition(object);
                return PagerAdapter.POSITION_NONE;
            }

        };

        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public int getItemPosition(Object object) {
//                return super.getItemPosition(object);
                return PagerAdapter.POSITION_NONE;
            }
        };


        viewPager.setAdapter(fragmentStatePagerAdapter);
//        viewPager.setAdapter(fragmentPagerAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                LogUtils.d("onPageScrolled" + position);
//                if (position > 1) {
//                    titleLl.setVisibility(View.GONE);
//                } else {
//                    titleLl.setVisibility(View.VISIBLE);
//                }



            }

            @Override
            public void onPageSelected(int position) {
                tabMenuClicked(position);
                if (position == 1) {
                    remove2();
                }
                LogUtils.d("onPageSelected" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                LogUtils.d("onPageScrollStateChanged" + state);
            }
        });

    }

    private void tabMenuClicked(int position) {
        resetTabMenuStyle();
        switch (position) {
            case 0:
                tv1.setText("选项一");
                tv2.setVisibility(View.VISIBLE);
                tv1.setSelected(true);
                break;
            case 1:
                tv1.setText("选项一");
                tv2.setVisibility(View.VISIBLE);
                tv2.setSelected(true);
                break;
            case 2:
                tv2.setVisibility(View.GONE);
                tv1.setText("title33333");
                break;
        }
    }

    private void resetTabMenuStyle() {
        tv1.setSelected(false);
        tv2.setSelected(false);
    }
}
